/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gra.mechanika.Obiekty;

import gra.OBJloader.Face;
import gra.OBJloader.Model;
import gra.OBJloader.Model_loader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.geom.Vector2f;



/**
 *
 * @author Grog
 */
public abstract class Obiekt {
    private float x;
    private float y;
    private float z;
    private float dx;
    private float dy;
    private float dz;
    private float speed;
    private Vector3f pozycja;
    
    
    private String name;

    
    
  
    
    
    
    /**
     * @return the x
     */
    public float getX() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(float x) {
        this.x = x;
    }

    /**
     * @return the y
     */
    public float getY() {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(float y) {
        this.y = y;
    }

    /**
     * @return the z
     */
    public float getZ() {
        return z;
    }

    /**
     * @param z the z to set
     */
    public void setZ(float z) {
        this.z = z;
    }

    /**
     * @return the dx
     */
    public float getDx() {
        return dx;
    }

    /**
     * @param dx the dx to set
     */
    public void setDx(float dx) {
        this.dx = dx;
    }

    /**
     * @return the dy
     */
    public float getDy() {
        return dy;
    }

    /**
     * @param dy the dy to set
     */
    public void setDy(float dy) {
        this.dy = dy;
    }

    /**
     * @return the dz
     */
    public float getDz() {
        return dz;
    }

    /**
     * @param dz the dz to set
     */
    public void setDz(float dz) {
        this.dz = dz;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the speed
     */
    public float getSpeed() {
        return speed;
    }

    /**
     * @param speed the speed to set
     */
    public void setSpeed(float speed) {
        this.speed = speed;
    }

    /**
     * @return the pozycja
     */
    public Vector3f getPozycja() {
        return pozycja;
    }

    /**
     * @param pozycja the pozycja to set
     */
    public void setPozycja(Vector3f pozycja) {
        this.pozycja = pozycja;
    }
    
    /**
     * @param pozycja the pozycja to set
     */
    public void setPozycja(float x,float y,float z) {
        this.pozycja = new Vector3f(x, y, z);
    }
    
    
    public static File f;
    public Model model;
    public int model_Obj;
    
    public float dlugosc;
    
    

    public void stworz_model() {
        model_Obj = glGenLists(1);
		glNewList(model_Obj, GL_COMPILE);
		{    
        Model_loader ml = new Model_loader();
        try {
            model = ml.Model_load(f);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Statek.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Statek.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
         }
               // glColor3f(1.0f, 1.0f, 1.0f);
                 
                
                int currentTexture=0;        
                 
                
                
                glBegin(GL_TRIANGLES);
               
                for (Face face : model.faces) { 
                    
                    System.out.println("Tekstura  id:" +face.texture);
                    
                    if(face.texture!=currentTexture)
        {
             currentTexture = face.texture;
             glBindTexture(GL_TEXTURE_2D, currentTexture);
             System.out.println("Tekstura przypisana id:"+currentTexture);
             System.out.println(glIsTexture(currentTexture));
             
             
        }
                
                    
                                Vector2f t1 = model.texVert.get((int) face.texturei.x - 1);
				glTexCoord2f(t1.x, t1.y);             
                                Vector3f n1 = model.normals.get((int) face.normals.x - 1);
				glNormal3f(n1.x, n1.y, n1.z);
				Vector3f v1 = model.vertices.get((int) face.vertexi.x - 1);
				glVertex3f(v1.x, v1.y, v1.z);
                                
                                Vector2f t2 = model.texVert.get((int) face.texturei.y - 1);
				glTexCoord2f(t2.x, t2.y);
				Vector3f n2 = model.normals.get((int) face.normals.y - 1);
				glNormal3f(n2.x, n2.y, n2.z);
				Vector3f v2 = model.vertices.get((int) face.vertexi.y - 1);
				glVertex3f(v2.x, v2.y, v2.z);
                                
                                Vector2f t3 = model.texVert.get((int) face.texturei.z - 1);
				glTexCoord2f(t3.x, t3.y);
                                //glTexCoord2f(1, 1);
				Vector3f n3 = model.normals.get((int) face.normals.z - 1);
				glNormal3f(n3.x, n3.y, n3.z);
				Vector3f v3 = model.vertices.get((int) face.vertexi.z - 1);
				glVertex3f(v3.x, v3.y, v3.z);
                                
                                
                                System.out.println(face.texturei);
                                System.out.println("Coordy tekstur: t1x:"+ t1.x+",t1y:"+t1.y+", t2x:"+t2.x+",t2y:"+t2.y+", t3x:"+t3.x+",t3y:"+t3.y);
                                
                                
                                
                        }           
	glEnd();          
                glEndList();
       
               
               
                
        
    }
    
    public float getDlugosc(){     
        float poczatek = 0,koniec = 0;
        
        
        for(int i = 0;i<model.vertices.size();i++){
          
          
            if(model.vertices.get(i).z <poczatek){
                poczatek =  model.vertices.get(i).z;        
            
            }
            if(model.vertices.get(i).z >poczatek){
                koniec =  model.vertices.get(i).z;        
            
            }
       
                
        }    
        return dlugosc = Math.abs(poczatek)+Math.abs(koniec);
    }
    
    public void skaluj_Obj(int wielokrotnosc){
        
                 
        
    }
    
    public void wyswietl_Obj(){   
                  glTranslatef(getX(), getY(), getZ());
                  glTranslatef(0, 0, -3);
        
                //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                
		glCallList(model_Obj); 
               
    }
    
    
}
